package com.tankbattle.stage.body;


import com.tankbattle.enums.DirectionEnum;
import com.tankbattle.stage.body.buildings.BuildingsObject;
import com.tankbattle.stage.body.buildings.Steel;
import com.tankbattle.stage.body.buildings.Wall;
import com.tankbattle.stage.body.tank.Tank;
import com.tankbattle.utils.GetResource;

import java.applet.Applet;
import java.applet.AudioClip;

/**
 * @author 13797
 */
public class Bullet implements Runnable, GetResource {

    private int x;
    private int y;
    /**
     * type：防击杀队友
     * 0: from player
     * 1: from enemy
     */
    private int type;

    private DirectionEnum direction;
    private int speed = 12;
    private Boolean alive;
    private Boolean hitBuilding;
    private Boolean hitSteel;
    private BuildingsObject hitObject;
    private BuildingsObject boomPlace;

    public Bullet(int x, int y, DirectionEnum direction) {
        this.x = x;
        this.y = y;
        this.alive = true;
        this.type = -1;
        this.hitBuilding = false;
        this.hitSteel = false;
        this.direction = direction;
    }

    @Override
    public void run() {

        while (true) {

            try {
                Thread.sleep(50);
            } catch (Exception e) {
                e.printStackTrace();
            }
            switch (direction) {
                case DOWN:
                    y -= speed;
                    break;
                case LEFT:
                    x -= speed;
                    break;
                case UP:
                    y += speed;
                    break;
                case RIGHT:
                    x += speed;
                    break;
                default:
                    break;
            }
            if (this.alive == false) {
                return;
            }
            if (x < 0 || y < 0 || x > 520 - 8 || y > 520 - 8) {
                if (this.getType() == 0) {
                    AudioClip bgm = Applet.newAudioClip(getResource("bgm/hitLimit.wav"));
                    bgm.play();
                }
                this.alive = false;
                return;
            }
            switch (this.getDirection()) {
                case DOWN:
                    if ((Tank.tanks[x][y] == 2 || Tank.tanks[x + 7][y] == 2) ||
                            (Tank.tanks[x][y] == 3 || Tank.tanks[x + 7][y] == 3) ||
                            (Tank.tanks[x][y] == 4 || Tank.tanks[x + 7][y] == 4)) {
                        this.alive = false;
                        if (Tank.tanks[x][y] == 2 || Tank.tanks[x + 7][y] == 2) {
                            this.hitBuilding = true;
                            this.hitSteel = true;
                            for (int from = 0; from < StageBody.steels.size(); from++) {
                                Steel wall = (Steel) StageBody.steels.get(from);
                                if (wall.inside(x, y) || wall.inside(x + 7, y)) {
                                    setHitTargetXY(wall.getX(), wall.getY());
                                    break;
                                }
                            }
                            setBoomPlaceXY(x - 9, getHitObject().getY() + 32 - 10);
                        } else if (Tank.tanks[x][y] == 3 || Tank.tanks[x + 7][y] == 3) {
                            StageBody.homeAlive = false;
                        } else if (Tank.tanks[x][y] == 4 || Tank.tanks[x + 7][y] == 4) {
                            for (int from = 0; from < StageBody.walls.size(); from++) {
                                Wall wall = (Wall) StageBody.walls.get(from);
                                if (wall.getAlive()) {
                                    if (wall.inside(x, y) || wall.inside(x + 7, y)) {
                                        wall.setAlive(false);
                                        this.setX(wall.getX());
                                        this.setY(wall.getY());
                                        break;
                                    }
                                }
                            }
                            this.hitBuilding = true;
                        }
                    }
                    break;

                case LEFT:
                    if ((Tank.tanks[x][y] == 2 || Tank.tanks[x][y + 7] == 2) ||
                            (Tank.tanks[x][y] == 3 || Tank.tanks[x][y + 7] == 3) ||
                            (Tank.tanks[x][y] == 4 || Tank.tanks[x][y + 7] == 4)) {
                        this.alive = false;
                        if (Tank.tanks[x][y] == 2 || Tank.tanks[x][y + 7] == 2) {
                            this.hitBuilding = true;
                            this.hitSteel = true;
                            for (int from = 0; from < StageBody.steels.size(); from++) {
                                Steel wall = (Steel) StageBody.steels.get(from);
                                if (wall.inside(x, y) || wall.inside(x, y + 7)) {
                                    setHitTargetXY(wall.getX(), wall.getY());
                                    break;
                                }
                            }
                            setBoomPlaceXY(getHitObject().getX() + 32 - 10, y - 7);
                        } else if (Tank.tanks[x][y] == 3 || Tank.tanks[x][y + 7] == 3) {
                            StageBody.homeAlive = false;
                        } else if (Tank.tanks[x][y] == 4 || Tank.tanks[x][y + 7] == 4) {
                            for (int from = 0; from < StageBody.walls.size(); from++) {
                                Wall wall = (Wall) StageBody.walls.get(from);
                                if (wall.getAlive()) {
                                    if (wall.inside(x, y) || wall.inside(x, y + 7)) {
                                        wall.setAlive(false);
                                        this.setX(wall.getX());
                                        this.setY(wall.getY());
                                        break;
                                    }
                                }
                            }
                            this.hitBuilding = true;
                        }
                    }
                    break;

                case UP:
                    if ((Tank.tanks[x][y + 7] == 2 || Tank.tanks[x + 7][y + 7] == 2) ||
                            (Tank.tanks[x][y + 7] == 3 || Tank.tanks[x + 7][y + 7] == 3) ||
                            (Tank.tanks[x][y + 7] == 4 || Tank.tanks[x + 7][y + 7] == 4)) {
                        this.alive = false;
                        if (Tank.tanks[x][y + 7] == 2 || Tank.tanks[x + 7][y + 7] == 2) {
                            this.hitBuilding = true;
                            this.hitSteel = true;
                            for (int from = 0; from < StageBody.steels.size(); from++) {
                                Steel wall = (Steel) StageBody.steels.get(from);
                                if (wall.inside(x, y + 7) || wall.inside(x + 7, y + 7)) {
                                    setHitTargetXY(wall.getX(), wall.getY());
                                    break;
                                }
                            }
                            setBoomPlaceXY(x - 11, getHitObject().getY() - 10);
                        } else if (Tank.tanks[x][y + 7] == 3 || Tank.tanks[x + 7][y + 7] == 3) {
                            StageBody.homeAlive = false;
                        } else if (Tank.tanks[x][y + 7] == 4 || Tank.tanks[x + 7][y + 7] == 4) {
                            for (int from = 0; from < StageBody.walls.size(); from++) {
                                Wall wall = (Wall) StageBody.walls.get(from);
                                if (wall.getAlive()) {
                                    if (wall.inside(x, y + 7) || wall.inside(x + 7, y + 7)) {
                                        wall.setAlive(false);
                                        this.setX(wall.getX());
                                        this.setY(wall.getY());
                                        break;
                                    }
                                }
                            }
                            this.hitBuilding = true;
                        }
                    }
                    break;

                case RIGHT:
                    if ((Tank.tanks[x + 7][y] == 2 || Tank.tanks[x + 7][y + 7] == 2) ||
                            (Tank.tanks[x + 7][y] == 3 || Tank.tanks[x + 7][y + 7] == 3) ||
                            (Tank.tanks[x + 7][y] == 4 || Tank.tanks[x + 7][y + 7] == 4)) {
                        this.alive = false;
                        if (Tank.tanks[x + 7][y] == 2 || Tank.tanks[x + 7][y + 7] == 2) {
                            this.hitBuilding = true;
                            this.hitSteel = true;
                            for (int from = 0; from < StageBody.steels.size(); from++) {
                                Steel wall = (Steel) StageBody.steels.get(from);
                                if (wall.inside(x + 7, y) || wall.inside(x + 7, y + 7)) {
                                    setHitTargetXY(wall.getX(), wall.getY());
                                    break;
                                }
                            }
                            setBoomPlaceXY(getHitObject().getX() - 14, y - 6);
                        } else if (Tank.tanks[x + 7][y] == 3 || Tank.tanks[x + 7][y + 7] == 3) {
                            StageBody.homeAlive = false;
                        } else if (Tank.tanks[x + 7][y] == 4 || Tank.tanks[x + 7][y + 7] == 4) {
                            for (int from = 0; from < StageBody.walls.size(); from++) {
                                Wall wall = (Wall) StageBody.walls.get(from);
                                if (wall.getAlive()) {
                                    if (wall.inside(x + 7, y) || wall.inside(x + 7, y + 7)) {
                                        wall.setAlive(false);
                                        this.setX(wall.getX());
                                        this.setY(wall.getY());
                                        break;
                                    }
                                }
                            }
                            this.hitBuilding = true;
                        }
                    }
                    break;
                default:
                    break;
            }

        }

    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public DirectionEnum getDirection() {
        return direction;
    }

    public void setDirection(DirectionEnum direction) {
        this.direction = direction;
    }


    public Boolean getHitSteel() {
        return hitSteel;
    }

    public void setHitSteel(Boolean hitSteel) {
        this.hitSteel = hitSteel;
    }

    public Boolean isAlive() {
        return alive;
    }

    public void setAlive(Boolean alive) {
        this.alive = alive;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public Boolean isHitBuilding() {
        return hitBuilding;
    }

    public void setHitBuilding(Boolean hitBuilding) {
        this.hitBuilding = hitBuilding;
    }


    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public BuildingsObject getHitObject() {
        return hitObject;
    }

    public void setHitObject(BuildingsObject hitObject) {
        this.hitObject = hitObject;
    }


    public void setHitTargetXY(int x, int y) {
        if (this.hitObject == null) {
            this.hitObject = new BuildingsObject();
        }
        this.hitObject.setX(x);
        this.hitObject.setY(y);
    }

    public BuildingsObject getBoomPlace() {
        return boomPlace;
    }

    public void setBoomPlace(BuildingsObject boomPlace) {
        this.boomPlace = boomPlace;
    }

    public void setBoomPlaceXY(int x, int y) {
        if (this.boomPlace == null) {
            this.boomPlace = new BuildingsObject();
        }
        this.boomPlace.setX(x);
        this.boomPlace.setY(y);
    }
}
